/**
 * www.anchora.com Inc.
 * Copyright (c) 2011 All Rights Reserved.
 */
package com.test;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.apache.log4j.Logger;

/**
 *                       
 * @Filename: SaasResult.java
 *
 * @Description: 
 *
 * @Version: 1.0
 *
 * @Author: yfshen
 *
 * @Email: yfshen@anchora.me
 *
 *       
 * @History:<br>
 *<li>Author: yfshen</li>
 *<li>Date: 2011-12-13</li>
 *<li>Version: 1.0</li>
 *<li>Content: create</li>
 *
 */
public class Result {

    private static Logger logger = Logger.getLogger(Result.class);
    private JSONObject    result = new JSONObject();

    /**
     * 构建一个<code>SaasResult.java</code>
     */
    public Result() {
        result.put("returnCode", SystemEnums.SUCCESS.code());
        result.put("returnMessage", SystemEnums.SUCCESS.getMessage());
    }

    /**
     * 设置CODE和MSG
     * @param code
     * @param msg
     */
    public void setCodeAndMsg(String code, String msg) {
        result.put("returnCode", code);
        result.put("returnMessage", msg);
    }

    /**
     * 将数据加入到JSON中
     * @param key
     * @param value
     */
    public void add(String key, Object value) {
        result.put(key, value);
    }

    public void addList(List<?> list) {
        result.put("list", JsonUtil.toJson(list));
    }

    public void addCount(Long count) {
    	result.put("count", count);
    }

    public void addResult(Object obj) {
    	if(obj instanceof String) {
    		result.put("result", obj);
    	} else {
    		result.put("result", JsonUtil.toJson(obj));
    	}
    }

    public void addCurrentPage(Long currentPage) {
    	result.put("currentPage", currentPage);
    }

    public void addTotalPage(Long totalpage) {
    	result.put("totalPage", totalpage);
    }
    
    public JSONObject returnResult() {
        return result;
    }

    /**
     * 创建一个JSON
     * @param fieldMap
     * @return
     */
    public JSONObject createJson(String fieldMap) {
        JSONObject cJson = new JSONObject();
        if (fieldMap == null) {
            return cJson;
        }
        String[] fields = fieldMap.split(";");
        int lenght = fields.length;
        for (int i = 0; i < lenght; i++) {
            if (lenght <= 0) {
                continue;
            }
            String[] field = fields[i].split("=");
            String fromName = field[0];
            String toName = field.length == 1 ? field[0] : field[1];
            cJson.put(fromName, toName);
        }
        return cJson;
    }
    
    /**
     * 输出到界面的数据
     * @param response
     * @param value
     * @throws BusinessException
     */
    public void asynchronousPrintResult(HttpServletResponse response, Object value) {
        response.setContentType("text/html");
        response.setCharacterEncoding("UTF-8");
        PrintWriter writer = null;
        try {
            writer = response.getWriter();
            writer.print(value.toString());
            writer.flush();
            writer.close();
        } catch (IOException e) {
            logger.error("asynchronousPrintResult error!" + e);
        }
    }
}
